package com.weds.edwx.web;

import com.weds.core.annotation.Logs;
import com.weds.edwx.entity.UpdateImgPathEntity;
import com.weds.edwx.service.SystemService;
import com.weds.edwx.service.UploadFileService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/filecontroller")
public class UploadFileController {

    @Autowired
    private UploadFileService uploadFileService;

    @Autowired
    private SystemService systemService;

    @Autowired
    ServletContext context;

    private Logger log = LogManager.getLogger();

    @Logs
    @ApiOperation(value = "PC端图片上传接口", notes = "PC端图片上传接口")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiImplicitParam(name = "Authorization", required = false)
    public void upload(HttpServletRequest request, HttpServletResponse response){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        MultipartHttpServletRequest multiReq = multipartResolver.resolveMultipart(request);
        String waichLx = multiReq.getParameter("waichLx");
        String waichXh = multiReq.getParameter("waichXh");
        System.out.println("收到参数开始!");
        System.out.println(waichLx);
        System.out.println(waichXh);
        System.out.println("收到参数结束!");
        ///===============================================================================
        System.out.println("收到图片!");
        MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest)request;
        //得到文件map对象
        Map<String, MultipartFile> files = Murequest.getFileMap();
        ///获取到项目根路径用于存储上传的图片
        String projectRootPath =System.getProperty("user.dir");
        System.out.println("项目根路径"+projectRootPath);
        //文件名
        String fileName="";
        //文件对象
        File tagetFile;
        //多张图片名用逗号拼接
        String imgpathStr="";
        ///循环所有文件
        for(MultipartFile file :files.values()){
            //获取到文件名
            fileName=file.getOriginalFilename();
            //3新名称(uuid随机数加上后缀名)
            String newfileName= (UUID.randomUUID().toString().replaceAll("-", ""))+fileName.substring(fileName.lastIndexOf("."));

            //创建文件对象
            tagetFile = new File(projectRootPath+"/image/"+newfileName);
            //文件名不存在 则新建文件，并将文件复制到新建文件中
            if(!tagetFile.exists()){
                    try {
                        tagetFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        //往磁盘写入图片
                        file.transferTo(tagetFile);
                        imgpathStr+=".\\image\\"+newfileName;
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
        //========================================================================================
            ///根据lx  xh  更新数据库的图片路径字段值
            //waichLx  0:请假 1:出差 2:外出 3:加班 4:调班 5:补出勤
            UpdateImgPathEntity uipe=new UpdateImgPathEntity();
            uipe.setImgPath(imgpathStr);
            uipe.setWaichXh(waichXh);
            //=======================================================
            if(waichLx.equals("0") || waichLx.equals("1") || waichLx.equals("2")){
                    uploadFileService.updateImgPathQingjia(uipe);
            }else if(waichLx.equals("3")){
                    uploadFileService.updateImgPathJiaban(uipe);
            }else if(waichLx.equals("4")){
                    uploadFileService.updateImgPathTiaoban(uipe);
            }
        //==========================================================================================
        System.out.println("接收完毕");
    }


}